package com.example.administrator.myapplication.View.Activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.myapplication.Bean.User;
import com.example.administrator.myapplication.Presenter.IMinePresenter;
import com.example.administrator.myapplication.Presenter.MinePresenter;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.View.Fragment.IMineView;
import com.example.administrator.myapplication.View.layout.MineItemView;
import com.google.gson.Gson;

/**
 * Created by Administrator on 2019/10/9 0009.
 */

public class EditProfileActivity extends AppCompatActivity implements IMineView,View.OnClickListener{
    private LinearLayout ll_root;
    private TextView toolbar_title;
    private TextView tv_back;
    private TextView tv_save;
    private Long user_id;
    private String property;
    private String value;
    private IMinePresenter iminePresenter;
    private MineItemView myOneLineView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_profile);
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
            getWindow().setStatusBarColor(Color.parseColor("#d16d26"));
        }
        initView();
    }
    private void initView(){
        property = getIntent().getStringExtra("property");
        value = getIntent().getStringExtra("value");
        user_id = getIntent().getLongExtra("id",-1);
        ll_root = findViewById(R.id.ll_root);
        tv_back = findViewById(R.id.toolbar_back);
        tv_save = findViewById(R.id.toolbar_save);
        tv_back.setOnClickListener(this);
        tv_save.setOnClickListener(this);
        iminePresenter = new MinePresenter(this);
        if(user_id!=-1&&property!=null&&value!=null){
            toolbar_title = findViewById(R.id.toolbar_title);
            toolbar_title.setText("修改"+property);
            myOneLineView = new MineItemView(this)
                    .init(R.drawable.ic_user_profile,property,true,false)
                    .setEditContent(value);
            ll_root.addView(myOneLineView);
        }
    }

    private User getUser(){
        value = myOneLineView.getRightContent();
        User user = new User();
        user.setUserId(user_id);
        switch (property){
            case "用户名":
                user.setUserName(value);
                break;
            case "性别":
                user.setSex(value);
                break;
            case "电话":
                user.setPhonenumber(value);
                break;
            case "Email":
                user.setEmail(value);
                break;
        }
        return user;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.toolbar_back:
                finish();
                break;
            case R.id.toolbar_save:
                iminePresenter.updateProfile(getUser());
                break;
        }
    }

    @Override
    public void showProfile(User user) {
        SharedPreferences.Editor editor = getSharedPreferences("currUser", Context.MODE_PRIVATE).edit();
        editor.putString("userJson", new Gson().toJson(user));
        editor.apply();
        finish();
    }

    @Override
    public void showError(String error) {
        Toast.makeText(this,error,Toast.LENGTH_LONG).show();
    }

    @Override
    public void showSuccess(String msg) {
        Toast.makeText(this,msg,Toast.LENGTH_LONG).show();
        iminePresenter.getProfile();
    }
}
